package com.clgc.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clgc.eduservice.entity.EduTask;
import com.clgc.eduservice.entity.vo.KcbjVo;
import com.clgc.eduservice.service.EduTaskService;
import com.clgc.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 作业表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/eduservice/edu-task")
@Slf4j
public class EduTaskController {

    @Autowired
    private EduTaskService eduTaskService;

    @ApiOperation("添加作业")
    @PutMapping(value = "/task")
    public R insert(@RequestBody EduTask eduTask) {
        boolean flag = eduTaskService.save(eduTask);
        return flag ? R.ok() : R.error().message("添加作业失败");
    }

    @ApiOperation("更新作业信息")
    @PostMapping(value = "/task")
    public R update(@RequestBody EduTask eduTask) {
        boolean flag = eduTaskService.updateById(eduTask);
        return flag ? R.ok() : R.error().message("作业更新失败");
    }

    @DeleteMapping(value = "/{ids}")
    public R deleteByIds(@PathVariable ArrayList<String> ids) {
        boolean flag = eduTaskService.removeByIds(ids);
        return flag ? R.ok() : R.error().message("作业删除失败");
    }

    @GetMapping(value = "/tasks")
    public R selects(@RequestParam Map<String, String> term) {
        List<EduTask> tasks = eduTaskService.selects((HashMap<String, String>) term);
        return tasks.size() >= 0 ? R.ok().data("tasks", tasks) : R.error().message("作业查询失败");
    }

    @ApiOperation("分页查询作业")
    @GetMapping("getTaskByPage/{page}/{size}")
    public R getTaskByPage(@PathVariable long page, @PathVariable long size) {
        Page<EduTask> eduTaskPage = new Page<>(page, size);
        eduTaskService.page(eduTaskPage, null);
        return R.ok().data("page", eduTaskPage);
    }

    @ApiOperation("查询课程下的对应作业信息")
    @GetMapping(value = "/kcbjtasks/{courseId}")
    public R getKcbjTaskByCourseId(@PathVariable("courseId") String courseId) {
        List<KcbjVo> list = eduTaskService.getKcbjTaskByCourseId(courseId);
        return R.ok().data("list", list);
    }
}

